
package com.springcloud.sc.ucpv5.client.application.message;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

import com.alibaba.fastjson.JSONArray;
import com.springcloud.sc.comm.core.constant.CommonConstants;
import com.springcloud.sc.ucp.v5.api.feign.UcpRemoteUserService;
import com.springcloud.sc.ucp.v5.api.vo.UserVO;
import com.springcloud.sc.ucpv5.client.common.CmdDefine;
import com.springcloud.sc.ucpv5.client.commondata.MyGroup;
import com.springcloud.sc.ucpv5.client.commondata.MyNode;
import com.springcloud.sc.ucpv5.client.commondata.MyUser;
import com.springcloud.sc.ucpv5.client.commondata.data.DataChange;
import com.springcloud.sc.ucpv5.client.commondata.data.Group;
import com.springcloud.sc.ucpv5.client.commondata.data.GroupQueryRsp;
import com.springcloud.sc.ucpv5.client.commondata.data.GroupStatusQueryRsp;
import com.springcloud.sc.ucpv5.client.commondata.data.GroupUserProperty;
import com.springcloud.sc.ucpv5.client.commondata.data.GroupUserStatusQueryRsp;
import com.springcloud.sc.ucpv5.client.commondata.data.MsgHeader;
import com.springcloud.sc.ucpv5.client.commondata.data.Node;
import com.springcloud.sc.ucpv5.client.commondata.data.NodeStatusQueryRsp;
import com.springcloud.sc.ucpv5.client.commondata.data.StatusStruct;
import com.springcloud.sc.ucpv5.client.commondata.data.User;
import com.springcloud.sc.ucpv5.client.commondata.data.UserInfo;
import com.springcloud.sc.ucpv5.client.commondata.data.UserInfoQueryRsp;
import com.springcloud.sc.ucpv5.client.commondata.data.UserQueryRsp;
import com.springcloud.sc.ucpv5.client.contentmain.MainConstant;
import com.springcloud.sc.ucpv5.client.module.datamanager.BaseDataManager;
import com.springcloud.sc.ucpv5.client.util.ThreadUtil;
import com.springcloud.sc.ucpv5.service.JobUcpScheduleService;
import com.springcloud.sc.ucpv5.service.UcpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

@Component
public class MesssageHandle implements IMessageHandler {

	private final static Logger logger = LoggerFactory.getLogger(MesssageHandle.class);
	private static MesssageHandle instance = new MesssageHandle();

	@Autowired
	private JobUcpScheduleService jobUcpScheduleService;

	@Autowired
	private UcpService ucpService;

	@Autowired
	private UcpRemoteUserService ucpRemoteUserService;

	private MesssageHandle(){ }

	public static MesssageHandle getInstance() {
		return instance;
	}

	@PostConstruct
	public void init(){
		instance = this;
		instance.jobUcpScheduleService = this.jobUcpScheduleService;
		instance.ucpService = this.ucpService;
		instance.ucpRemoteUserService = this.ucpRemoteUserService;
	}

	public int handleMessage(final MsgHeader msgHeader) {

		try {
			switch (msgHeader.CmdID) {
			case CmdDefine.CMD_NODE_Query_Status: {
				NodeStatusQueryRsp rsp = new NodeStatusQueryRsp();
				rsp.dataContentDataToCommon(msgHeader.MsgContent);
				break;
			}
			case CmdDefine.CMD_Group_Query_Status:
			case CmdDefine.CMD_Group_Query_Status_Single: {
				GroupStatusQueryRsp rsp = new GroupStatusQueryRsp();
				rsp.dataContentDataToCommon(msgHeader.MsgContent);
				// 更新组状态信息
				for (StatusStruct status : rsp.StatusList) {
					MyGroup group = BaseDataManager.getInstance().groupGet(status.Number);
					if (group == null) {
						continue;
					}
					if (group.setStatus(status)) {
						;
					}
				}
				break;
			}
			case CmdDefine.CMD_Group_Query_UserOnLine_UserAllSize:
			// 组统计属性改变
			{
				Callable<Object> call = new Callable<Object>() {
					public Object call() {
						try {
							handle_统计组数目修改(msgHeader);
						} catch (Exception e) {
							// TODO: handle exception
						}
						return null;
					}
				};
				ThreadUtil.getInstance().Call(call);
				break;
			}
			case CmdDefine.CMD_Group_Query_Mermber_Status:
			case CmdDefine.CMD_User_Query_Status: {
				Callable<Object> call = new Callable<Object>() {
					public Object call() {
						try {
							handle_组用户状态修改_New(msgHeader);
						} catch (Exception e) {
							// TODO: handle exception
						}
						return null;
					}
				};
				ThreadUtil.getInstance().Call(call);
				break;
			}

			case CmdDefine.CMD_Group_Query_Media_Status:
			case CmdDefine.CMD_Group_Query_Media_Status_All: {
				Callable<Object> call = new Callable<Object>() {
					public Object call() {
						try {
							handle_媒体用户状态修改(msgHeader);
						} catch (Exception e) {
							// TODO: handle exception
						}
						return null;
					}
				};
				ThreadUtil.getInstance().Call(call);
				break;
			}

			case CmdDefine.CMD_Gps_CurrentQuery:
			case CmdDefine.CMD_Gps_QueryBaseInfo: {
				;
				break;
			}

			// 用户数据变更操作
			/**
			 * string nodeNumber; //节点号码, 物理节点号码 string physicsNodeNumber;
			 * //逻辑节点号码 数据所在在节点号码 long changeType; // 数据改变类型, 0 表示节点内容, 1
			 * 表示组内容改变, 2 表示用户内容改变, 3 视频用户内容, 4 表示组内成员改变 long changeMode; //
			 * 数据改变类型, 0 表示增加, 1 表示修改, 2 表示删除, 3 用户停机, 4用户复机 string
			 * changeNumber; // 填写改变成员的号码,比如节点号码,组号码,用户号码 MsgBody dataContent;
			 * //如果changeType 为0, 内容为Node, 1, 内容为Group, 2 内容为UserInfo, 3
			 * 内容为MediaUser
			 */
			case CmdDefine.CMD_DataChange_Info: {

				Callable<Object> call = new Callable<Object>() {
					public Object call() {
						try {
							int returnInt = handle_数据变更操作(msgHeader);
						} catch (Exception e) {
							e.printStackTrace();
						}
						return null;
					}
				};
				ThreadUtil.getInstance().Call(call);

				break;
			}

			// 异步查询用户详细信息返回
			case CmdDefine.CMD_User_Query_Info: {
				Callable<Object> call = new Callable<Object>() {
					public Object call() {
						try {
							handle_用户详细查询数据上报(msgHeader);
						} catch (Exception e) {
							// TODO: handle exception
						}
						return null;
					}
				};
				ThreadUtil.getInstance().Call(call);
				break;
			}

			// 异步查询用户列表信息返回
			case CmdDefine.CMD_User_Query_List:
			case CmdDefine.CMD_Query_Group_User:
			case CmdDefine.CMD_User_Query_Info_detail: {
				Callable<Object> call = new Callable<Object>() {
					public Object call() {
						try {
							handle_用户查询数据上报(msgHeader);
						} catch (Exception e) {
							e.printStackTrace();
						}
						return null;
					}
				};
				ThreadUtil.getInstance().Call(call);

				break;
			}
			case CmdDefine.CMD_Group_Query_Mermber_Status_Simple: {

				Callable<Object> call = new Callable<Object>() {
					public Object call() {
						try {
							handle_组用户状态修改_New(msgHeader);
						} catch (Exception e) {
							// TODO: handle exception
						}
						return null;
					}
				};
				ThreadUtil.getInstance().Call(call);
				break;
			}
			// 异步查询用户列表信息返回
			case CmdDefine.CMD_Load_User_Group_Table: {
				break;
			}

			case CmdDefine.CMD_Group_Query_List:
			case CmdDefine.CMD_Group_Query_List_SubGroup: {
				Callable<Object> call = new Callable<Object>() {
					public Object call() {
						try {
							handle_查询组内列表(msgHeader);
						} catch (Exception e) {
							// TODO: handle exception
						}
						return null;
					}
				};
				ThreadUtil.getInstance().Call(call);

				break;
			}
			case CmdDefine.CMD_Group_Query_Single: {
				Callable<Object> call = new Callable<Object>() {
					public Object call() {
						try {
							handle_查询组详细信息(msgHeader);
						} catch (Exception e) {
							// TODO: handle exception
						}
						return null;
					}
				};
				ThreadUtil.getInstance().Call(call);
				break;
			}
			case CmdDefine.CMD_Query_国标设备: {
				;
				break;
			}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	private int handle_用户详细查询数据上报(MsgHeader msgHeader) {
		UserInfoQueryRsp rsp = new UserInfoQueryRsp();
		rsp.dataContentDataToCommon(msgHeader.MsgContent);

		// 找到用户,设置用户详细,然后出发刷新
		if (rsp.Result != 0) {
			return 0;
		}
		MyNode node = BaseDataManager.getInstance().nodeGet(msgHeader.NodeNumber);
		MyUser user = BaseDataManager.getInstance().userGet(rsp.Info.UserTelno);
		if (user == null) {
			// 增加
			user = new MyUser(rsp.Info, msgHeader.Physics_NodeNumber, msgHeader.NodeNumber, 0);
			// 更新包含用户内的组
			ArrayList<MyGroup> list = user.getGroupList();
			if (list.size() == 0) {
				// 加入节点中
				node.getListModel().addUser(user, true);
			} else {
				for (MyGroup tmp : list) {
					tmp.getListModel().addUser(user, true);
				}
			}
		} else {
			// 用户所属的组
			String groupList = user.getIdlData().GrpList;
			String groupList_new = rsp.Info.GrpList;
			rsp.Info.GrpList = groupList;
			user.setUserInfo(rsp.Info);
			user.dealToGroup(groupList_new, true);

			// 更新包含用户内的组
			ArrayList<MyGroup> list = user.getGroupList();
			for (MyGroup tmp : list) {
				tmp.getListModel().addUser(user, false);
			}
		}
		return 0;
	}

	private int handle_用户查询数据上报(MsgHeader msgHeader) {
		UserQueryRsp rsp = new UserQueryRsp();
		rsp.dataContentDataToCommon(msgHeader.MsgContent);

		// 找到用户,设置用户详细,然后出发刷新
		if (rsp.Result != 0) {
			logger.info("异步返回结果错误.");
			return 0;
		}
		// 处理用户所在的节点
		MyNode dataNode = BaseDataManager.getInstance().nodeGet(msgHeader.NodeNumber);
		for (User user_get : rsp.UserList) {
			if (user_get.UserTelno.trim().equals("")) {
				continue;
			}
			MyNode dataNode_current = dataNode;
			if (msgHeader.CmdID == CmdDefine.CMD_User_Query_Info_detail) {
				// 该查询中， 用户节点属性放到addtime 里面了， 需要处理流程是
				// 1.看该节点是存在, 不存在, 不管
				// 2.看该节点是否刷新过, 如果没刷新过, 则需要刷新组上来
				MyNode userNode = BaseDataManager.getInstance().nodeGet(user_get.addtime);
				if (userNode != null) {
					BaseDataManager.getInstance().nodeLoadData(userNode);
				} else {
					// 有可能服务器还不支持, 没有把节点号放入到user_get.addtime 里面
					// 所以保持使用原来节点
				}
				dataNode_current = userNode;
			}
			if (dataNode_current == null) {
				// 出现异节点用户, 且节点不可见情况
				BaseDataManager.getInstance().userPushData(msgHeader.Physics_NodeNumber, msgHeader.NodeNumber, user_get, false);
			} else {
				BaseDataManager.getInstance().userPushData(dataNode_current, user_get, false);
			}
		}
		return 0;
	}

	private int handle_组用户状态修改_New(MsgHeader msgHeader) {
		GroupUserStatusQueryRsp rsp = new GroupUserStatusQueryRsp();
		rsp.dataContentDataToCommon(msgHeader.MsgContent);

		HashMap<MyUser, ArrayList<StatusStruct>> statusMap = new HashMap<MyUser, ArrayList<StatusStruct>>();

		if (rsp == null || rsp.StatusList == null || rsp.StatusList.length <= 0) {
			return 0;
		}

		for (StatusStruct status : rsp.StatusList) {
			// if (status.Number.equals("2002") || status.Number.equals("2018"))
			// {
			// System.out.println("*****.*********收到组用户状态查询返回异步消息:" +
			// status.Number + ":" +
			// status.status + " CmdID:" + msgHeader.CmdID);
			// }
			if (status.status >= MainConstant.Status_GROUP_USER_STATUS_MAX) {
				continue;
			}

			if (status.status <= 0) {
				// 何青V5的时候, 用来判断用户在呼叫时,变成空闲使用
				status.status = MainConstant.Status_VLR_USER_STATUS_IDLE;
			}

			MyUser user = BaseDataManager.getInstance().userGet(status.Number);
			if (user == null) {
				// System.out.println("用户不存在,为何返回:" + status.Number);
				continue;
			}

			ArrayList<StatusStruct> tmp_ = statusMap.get(user);
			if (statusMap.get(user) == null) {
				tmp_ = new ArrayList<StatusStruct>();
				statusMap.put(user, tmp_);
			}
			tmp_.add(status);
		}

		ArrayList<MyGroup> 需要刷新的组列表 = new ArrayList<MyGroup>();
		for (MyUser user : statusMap.keySet()) {
			ArrayList<StatusStruct> tmp_ = statusMap.get(user);
			if (tmp_ != null) {
				// 20150227 终端支持多路呼叫, 所以状态也取多路
				if (true) {
					// 清除多余状态,后台总会上报四个线路状态
					ArrayList<StatusStruct> tmp__ = new ArrayList<StatusStruct>();
					boolean isExistCall = false;
					for (StatusStruct status : tmp_) {
						boolean isExist = false;
						for (StatusStruct status_ : tmp__) {
							// 后台空闲时,有可能带两个空闲状态,同时空闲状态都是带对端号码的
							// 所以判断重复的时候,只有呼叫中才判断对端号码
							if (status.status == status_.status
									&& status.Number.equals(status_.Number)
									&& ((status.status != MainConstant.Status_GROUP_USER_STATUS_TEL_BUSY) || (status.status == MainConstant.Status_GROUP_USER_STATUS_TEL_BUSY && status.PeerNumber
											.equals(status_.PeerNumber)))) {
								isExist = true;
								continue;
							}
						}
						if (!isExist) {
							if (status.status != MainConstant.Status_GROUP_USER_STATUS_IDLE && status.status != MainConstant.Status_GROUP_USER_STATUS_LOGOUT
									&& status.status != MainConstant.Status_GROUP_USER_STATUS_NOT_EXIST && status.status != MainConstant.Status_GROUP_USER_STATUS_UNREACH) {
								isExistCall = true;
							}
							tmp__.add(status);
						}
					}

					if (isExistCall) {
						// 去掉空闲,离线等状态
						tmp_ = new ArrayList<StatusStruct>();
						for (StatusStruct status : tmp__) {
							if (status.status != MainConstant.Status_GROUP_USER_STATUS_IDLE && status.status != MainConstant.Status_GROUP_USER_STATUS_LOGOUT
									&& status.status != MainConstant.Status_GROUP_USER_STATUS_NOT_EXIST && status.status != MainConstant.Status_GROUP_USER_STATUS_UNREACH) {
								tmp_.add(status);
							}
						}
					} else {
						// 如果有非离线的,去掉离线等的
						ArrayList<StatusStruct> tmp_free = new ArrayList<StatusStruct>();
						for (StatusStruct status : tmp__) {
							if (status.status != MainConstant.Status_GROUP_USER_STATUS_LOGOUT && status.status != MainConstant.Status_GROUP_USER_STATUS_NOT_EXIST
									&& status.status != MainConstant.Status_GROUP_USER_STATUS_UNREACH) {
								tmp_free.add(status);
							}
						}

						if (tmp_free.size() <= 0) {
							tmp_ = tmp__;
						} else {
							tmp_ = tmp_free;
						}
					}
				}

				if (user.setStatus(tmp_)) {
					// 更新包含用户内的组
					ArrayList<MyGroup> list = user.getGroupList();
					for (MyGroup tmp : list) {
						if (!需要刷新的组列表.contains(tmp)) {
							需要刷新的组列表.add(tmp);
						}
					}
				}
			}
		}

		for (MyGroup tmp : 需要刷新的组列表) {
			// 更新组内用户在线数目
			tmp.getListModel().updateUserOwnOnLine();
			tmp.getListModel().updateUserOwnTotal();
		}

		statusMap.clear();
		statusMap = null;

		return 0;

	}

	private int handle_统计组数目修改(MsgHeader msgHeader) {
		GroupStatusQueryRsp rsp = new GroupStatusQueryRsp();
		rsp.dataContentDataToCommon(msgHeader.MsgContent);

		MyNode dataNode = BaseDataManager.getInstance().nodeGet(msgHeader.NodeNumber);

		// 更新组统计信息
		// System.out.println("上报统计组数目长度:" + rsp.StatusList.length);
		for (StatusStruct status : rsp.StatusList) {
			MyGroup group = BaseDataManager.getInstance().groupGet(status.Number);
			if (group == null) {
				// System.out.println("组不存在,为何返回:" + status.Number + " size:"+
				// rsp.StatusList.length);
				continue;
			}
			// System.out.println("上报统计组数目:" + status.Number + " 在线用户数" +
			// status.status + " 用户总数" + status.PeerNumber);
			boolean isChange = false;
			try {
				if (group.getListModel().setUserOnLine(status.status)) {
					isChange = true;
				}

				if (group.getListModel().setUserAllSize(Integer.parseInt(status.PeerNumber))) {
					isChange = true;
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			// System.out.println("上报统计组数目Change End");
		}
		return 0;
	}

	private int handle_媒体用户状态修改(MsgHeader msgHeader) {
		GroupUserStatusQueryRsp rsp = new GroupUserStatusQueryRsp();
		rsp.dataContentDataToCommon(msgHeader.MsgContent);

		MyNode dataNode = BaseDataManager.getInstance().nodeGet(msgHeader.NodeNumber);

		if (rsp == null || rsp.StatusList == null) {
			return 0;
		}
		for (StatusStruct status : rsp.StatusList) {
			// if(status.Number.indexOf("1011") >= 0){
			// System.out.println("*******.*******收到组用户视频上传状态查询返回异步消息: cmdid:" +
			// msgHeader.CmdID + " number:" + status.Number + ":" +
			// status.status);
			// }
			String[] tmp = status.Number.split("#");

			if (tmp.length < 0) {
				continue;
			}

			// 31个F
			if (tmp[0].equals("FFFF")) {
				// 提示刷新所有视频用户的视频状态,置为离线
				BaseDataManager.getInstance().userUpdateAllUserMediaStatus(dataNode);
				break;
			}

			MyUser user = BaseDataManager.getInstance().userGet(tmp[0]);
			if (user == null) {
				continue;
			}

			// 带子通道的通道号
			String channel = "0";
			if (tmp.length == 2) {
				channel = tmp[1];
			}

			boolean isStatusChange = false;
			// ///////// -------------------------根据不同命令码做响应的处理 2016/08/17
			if (msgHeader.CmdID == CmdDefine.CMD_Group_Query_Media_Status) {
				// if(status.Number.indexOf("2044") >= 0){
				// System.out.println("*******.*******收到组用户视频上传状态查询返回异步消息:" +
				// status.Number + ":" + status.status);
				// }

				MyUser user_channel = user;
				if (channel.equals("")) {
					;
				} else {
					// 子通道用户
					user_channel = user.getChildren(channel);
				}
				if (status.status == 2) {
					if (!user_channel.isVideoUp()) {
						isStatusChange = true;
						user_channel.addStatusFalg(MyUser.STATUS_VIDEO_UP);
					}
				} else {
					if (user_channel.isVideoUp()) {
						isStatusChange = true;
						user_channel.removeStatusFalg(MyUser.STATUS_VIDEO_UP);
					}
				}
			} else if (msgHeader.CmdID == CmdDefine.CMD_Group_Query_Media_Status_All) {
				String status_string = Integer.toBinaryString(status.status);
				byte[] status_bytes = status_string.getBytes();
				if (status_string.getBytes().length <= 0) {
					continue;
				}

				// 更新用户
				if (!user.isSupportChannel()) {
					// 更新第一位
					if (status_bytes[status_bytes.length - 1] == 48) {
						if (user.isVideoUp()) {
							isStatusChange = true;
							user.removeStatusFalg(MyUser.STATUS_VIDEO_UP);
						}
					} else if (status_bytes[status_bytes.length - 1] == 49) {
						if (!user.isVideoUp()) {
							isStatusChange = true;
							user.addStatusFalg(MyUser.STATUS_VIDEO_UP);
						}
					}
				} else {
					// 带子通道的用户
					ArrayList<MyUser> children = user.getChildren();
					if (children == null || children.size() <= 0) {
						continue;
					}
					for (MyUser child : children) {
						try {
							if (child.getChannel() == null) {
								continue;
							}
							int channel_num = Integer.valueOf(child.getChannel());
							int byteN = -1;
							if (false && MyUser.is子用户通道从1开始(child)) {
								if ((status_bytes.length) < channel_num) {
									if (child.isVideoUp()) {
										isStatusChange = true;
										child.removeStatusFalg(MyUser.STATUS_VIDEO_UP);
									}
									continue;
								}
								byteN = status_bytes[status_bytes.length - channel_num];
							} else {
								if ((status_bytes.length) < channel_num + 1) {
									if (child.isVideoUp()) {
										isStatusChange = true;
										child.removeStatusFalg(MyUser.STATUS_VIDEO_UP);
									}
									continue;
								}

								byteN = status_bytes[status_bytes.length - channel_num - 1];
							}

							// 更新第一位
							if (byteN == 48) {
								if (child.isVideoUp()) {
									isStatusChange = true;
									child.removeStatusFalg(MyUser.STATUS_VIDEO_UP);
								}
							} else if (byteN == 49) {
								if (!child.isVideoUp()) {
									isStatusChange = true;
									child.addStatusFalg(MyUser.STATUS_VIDEO_UP);
								}
							}
						} catch (Exception e) {
							e.printStackTrace();
							if (child.isVideoUp()) {
								child.removeStatusFalg(MyUser.STATUS_VIDEO_UP);
							}
						}
					}
				}
			}
		}

		return 0;
	}

	private int handle_数据变更操作(MsgHeader msgHeader) {
		MyNode dataNode = BaseDataManager.getInstance().nodeGet(msgHeader.NodeNumber);

		DataChange rsp = new DataChange();
		rsp.dataContentDataToCommon(msgHeader.MsgContent);
		DataChange dataChange = rsp;

		switch (dataChange.changeType) {
		case 0: {
			// 节点内容修改
			Node nodeRsp = new Node();
			nodeRsp.dataContentDataToCommon(dataChange.dataContent);
			switch (dataChange.changeMode) {
			case 0:
			case 1:
			case 2:
				MyNode node_tmp = BaseDataManager.getInstance().nodePushData(nodeRsp, dataChange.changeMode == 2);
			}
			break;
		}
		case 1: {
			// 组内容修改
			Group groupRsp = new Group();
			groupRsp.dataContentDataToCommon(dataChange.dataContent);
			switch (dataChange.changeMode) {
			case 0:
			case 1:
			case 2: {
				MyGroup group = BaseDataManager.getInstance().groupPushData(dataNode, groupRsp, dataChange.changeMode == 2);
				if (group != null) {
					//groupType == 2 表示为应急组
					if(group.getIdlData().getGroupType() == 2 ){
						jobUcpScheduleService.saveEntityByMyGroup(group , "pds_group_set");
						ucpService.getAllUserInfoListTreeByGroup();
					}
				}else if(dataChange.changeMode == 2){
					if(groupRsp.getGroupType() == 2 ){
						Map map = new HashMap<>();
						map.put("operType" , 2 );
						map.put("number",groupRsp.getGroupTelno());
						map.put("sign",groupRsp.getKeep1());
						Object json = JSONArray.toJSON(  map );
						jobUcpScheduleService.saveJobUcpSchedule( groupRsp.getGroupTelno() , json.toString() , "pds_group_del"  );
						ucpService.getAllUserInfoListTreeByGroup();
					}
				}
				break;
			}
			}
			break;
		}
		case 2:
			switch (dataChange.changeMode) {
			case 0:
			case 1:
				// 用户内容修改
				UserInfo userRsp = new UserInfo();
				userRsp.dataContentDataToCommon(dataChange.dataContent);
				BaseDataManager.getInstance().userPushData(dataNode, userRsp);

			{
				MyUser user = BaseDataManager.getInstance().userGet(userRsp.getUserTelno());
				user.getUserInfo(true);
				ArrayList<MyGroup> myGroupLists = user.getGroupList();
				if(myGroupLists.isEmpty()){
					if(user.isDispather()){
						try{
							// wsy 不确定userServer是会否为空
							String logicNodeNumber = "100";
							//保存用户信息至WEB系统
							UserVO userVO = new UserVO();
							userVO.setUserId( userRsp.getUserTelno() );
							userVO.setUsername( userRsp.getUserTelno() );
							userVO.setPassword( userRsp.getPassword() );
							userVO.setDeptId( logicNodeNumber );
							userVO.setRoleId( userRsp.getGrpList() );
							userVO.setSystemId( CommonConstants.STATUS_NORMAL );
							boolean bool =  ucpRemoteUserService.saveUserAPI(  userVO );
						}catch (Exception e){
							e.printStackTrace();
						}
					}
				}else{
					myGroupLists.forEach(group -> {
						//调度台账号，需要进行用户的添加修改
						if(user.isDispather()){
							try{
								// wsy 不确定userServer是会否为空
								String logicNodeNumber = group.getLogicNodeNumber();
								//保存用户信息至WEB系统
								UserVO userVO = new UserVO();
								userVO.setUserId( userRsp.getUserTelno() );
								userVO.setUsername( userRsp.getUserTelno() );
								userVO.setPassword( userRsp.getPassword() );
								userVO.setDeptId( logicNodeNumber );
								userVO.setRoleId( userRsp.getGrpList() );
								userVO.setSystemId( CommonConstants.STATUS_NORMAL );
								boolean bool =  ucpRemoteUserService.saveUserAPI(  userVO );
							}catch (Exception e){
								e.printStackTrace();
							}
						}
						if (group != null && group.getIdlData().getGroupType() == 2) {
							jobUcpScheduleService.saveEntityByUserInfo(userRsp, "pds_user_set");
							ucpService.getAllUserInfoListTreeByGroup();
						}
					});
				}

			}

				break;
			default:
				// 用户内容删除
				User userBaseRspHolder = new User();
				userBaseRspHolder.dataContentDataToCommon(dataChange.dataContent);
				MyUser user = BaseDataManager.getInstance().userGet(userBaseRspHolder.UserTelno);
				if (user != null && user.isDispather()) {
					try{
						// wsy 不确定userServer是会否为空
						ucpRemoteUserService.deleteUserAPI( userBaseRspHolder.UserTelno );
					}catch(Exception e){
						e.printStackTrace();;
					}
					return 0;
				}
				switch (dataChange.changeMode) {
				case 2:
					BaseDataManager.getInstance().userPushData(dataNode, userBaseRspHolder, true);
					{
						ArrayList<MyGroup> myGroupLists = user.getGroupList();
						myGroupLists.forEach(group -> {
							if(group!=null && group.getIdlData().getGroupType() == 2){
								jobUcpScheduleService.deleteEntityByUserInfo( user.getUserInfo() , "pds_user_del");
								ucpService.getAllUserInfoListTreeByGroup();
							}
						});
					}
					/*String grpNumber = user.getIdlData().getGrpList();
					MyGroup grp = BaseDataManager.getInstance().groupGet( grpNumber );
					//表示是应急组里面删除用户
					if(grp!=null && grp.getIdlData().getGroupType() == 2){
						jobUcpScheduleService.saveEntityByUserInfo( user.getUserInfo() , "pds_user_set");
						ucpService.getAllUserInfoListTreeByGroup();
					}*/
					break;
				case 3:
					// 停机
					user.getIdlData().UserStatus = 4;
					break;
				case 4:
					// 复机
					user.getIdlData().UserStatus = 0;
					break;

				}
				break;
			}
			break;
		case 3:
			break;
		case 4: {
			// 组成员修改
			GroupUserProperty groupUserPropertyRspHolder = new GroupUserProperty();
			groupUserPropertyRspHolder.dataContentDataToCommon(dataChange.dataContent);
			// 组属性
			MyGroup group_ = BaseDataManager.getInstance().groupGet(groupUserPropertyRspHolder.GroupTelno);
			MyUser user_ = BaseDataManager.getInstance().userGet(groupUserPropertyRspHolder.UserTelno);

			if (group_ == null || user_ == null) {
				return 0;
			}

			switch (dataChange.changeMode) {
				case 0:
					//应急组成员添加

					// 加入组
					user_.dealToGroup(groupUserPropertyRspHolder.GroupTelno, true);
					group_.getListModel().pushUser(user_);
					user_.getUserInfo(true);
					//表示是应急组
					try {
						if(groupUserPropertyRspHolder.UserTelno.equals("")){
							System.out.println("");
						}
						if (group_.getIdlData().getGroupType() == 2) {
							jobUcpScheduleService.saveEntityByMyGroup(group_, "pds_group_set");
							ucpService.getAllUserInfoListTreeByGroup();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					break;
				case 2:
					// 离开组
					user_.dealToGroup(groupUserPropertyRspHolder.GroupTelno, false);
					group_.getListModel().popUser(user_);
					// 20170809 触发查询一下用户详细, 获取组列表信息
					user_.getUserInfo(true);
					//20190410添加应急组操作
					try {
						if (group_.getIdlData().getGroupType() == 2) {
							jobUcpScheduleService.saveEntityByMyGroup(group_, "pds_group_set");
							ucpService.getAllUserInfoListTreeByGroup();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				break;
			}
			group_.getListModel().updateUserOwnOnLine();
			group_.getListModel().updateUserOwnTotal();
		}
			break;
		case 5: {
			// 组成员内组修改

			// 组属性
			MyGroup group_ = null;
			MyGroup user_ = null;

			try {
				Group groupRsp = new Group();
				groupRsp.dataContentDataToCommon(dataChange.dataContent);

				// 组属性
				group_ = BaseDataManager.getInstance().groupGet(groupRsp.ParGrpTelno);
				user_ = BaseDataManager.getInstance().groupGet(groupRsp.GroupTelno);
			} catch (Exception e) {
				e.printStackTrace();
			}

			if (group_ == null || user_ == null) {
				return 0;
			}

			switch (dataChange.changeMode) {
			case 0:
				// 加入组
				group_.getListModel().addGroup(user_, false);

				break;
			case 2:
				// 离开组
				group_.getListModel().removeGroup(user_, false);

				// 如果离开组后, 该组的父组为空, 需要在节点中重新加入
				if (user_.getParents().isEmpty()) {
					//
					MyNode node = BaseDataManager.getInstance().nodeGet(user_.getLogicNodeNumber());
					if (node != null) {
						node.getListModel().addGroup(user_, false);
					}
				}
				break;
			}
		}
			break;
		}

		return 0;
	}

	private int handle_查询组内列表(MsgHeader msgHeader) {
		GroupQueryRsp rsp = new GroupQueryRsp();
		rsp.dataContentDataToCommon(msgHeader.MsgContent);

		// 找到用户,设置用户详细,然后出发刷新
		if (rsp.Result != 0) {
			System.out.println("查询组内列表.");
			return 0;
		}

		// 处理用户所在的节点
		MyNode dataNode = BaseDataManager.getInstance().nodeGet(msgHeader.NodeNumber);
		BaseDataManager.getInstance().nodePushData(dataNode, rsp);
		return 0;
	}

	private int handle_查询组详细信息(MsgHeader msgHeader) {
	GroupQueryRsp rsp = new GroupQueryRsp();
		rsp.dataContentDataToCommon(msgHeader.MsgContent);

		// 找到用户,设置用户详细,然后出发刷新
		if (rsp.Result != 0) {
			System.out.println("查询组内列表.");
			return 0;
		}

		// 处理用户所在的节点
	BaseDataManager.getInstance().groupPushData(msgHeader.Physics_NodeNumber, msgHeader.NodeNumber, rsp);
		return 0;
	}

}
